//
//  NetManager.m
//  Api
//
//  Created by Justsoso on 16/3/29.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "NetManager.h"

@implementation NetManager


+ (void)requestInfoByPersonId:(NSString *)personId
                 successBlock:(void(^)(NSString *))successBlock
                    failBlock:(void(^)(NSError *))failBlock
{
    NSString *httpUrl = @"http://apis.baidu.com/apistore/idservice/id";
    NSString *httpArg = [NSString stringWithFormat:@"id=%@",personId];
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, httpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    /* 设置请求头 */
    [request addValue: @"ff357caeef2d506f0c9c88651a4a7e9f" forHTTPHeaderField: @"apikey"];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError == nil)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSDictionary *dict1 = dict[@"retData"];
            NSString *result = [NSString stringWithFormat:@"生日:%@\n地址:%@\n性别:%@",dict1[@"birthday"],dict1[@"address"],dict1[@"sex"]];
            successBlock(result);
        }
        else
        {
            NSError *error = [NSError errorWithDomain:@"com.niit.apiweak" code:1 userInfo:@{NSLocalizedDescriptionKey:connectionError}];
            failBlock(error);
        }
    }];
}

+ (void)requestNumber:(NSString *)number
         successBlock:(void (^)(NSString *))successBlock
            failBlock:(void (^)(NSError *))failBlock
{
    NSString *httpUrl = @"http://apis.baidu.com/apistore/mobilenumber/mobilenumber";
    NSString *httpArg = [NSString stringWithFormat:@"phone=%@",number];
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, httpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"ff357caeef2d506f0c9c88651a4a7e9f" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
       if (error == nil) {
           NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
           NSDictionary *dict1 = dict[@"retData"];
           NSString *str = [NSString stringWithFormat:@"%@%@%@",dict1[@"province"],dict1[@"city"],dict1[@"supplier"]];
           successBlock(str);
           
       } else {
           NSError *error1 = [NSError errorWithDomain:@"com.niit.apiweak" code:1 userInfo:@{NSLocalizedDescriptionKey:error}];
           failBlock(error1);
       }
    }];
}

+ (void)requestWeatherByCityName:(NSString *)cityName
                    successBlock:(void (^)(NSString *result))successBlock
                       failBlock:(void (^)(NSError *error))failBlock
{
    NSString *httpUrl = @"http://apis.baidu.com/apistore/weatherservice/cityname";
    NSString *httpArg = [NSString stringWithFormat:@"cityname=%@",cityName];
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, httpArg];
    NSString* string2 = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString: string2];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"ff357caeef2d506f0c9c88651a4a7e9f" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
       if (error) {
           NSError *error1 = [NSError errorWithDomain:@"com.niit.apiweak" code:1 userInfo:@{NSLocalizedDescriptionKey:error}];
           failBlock(error1);
           
       }
       else
       {
           NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
           NSDictionary *dict1 = dict[@"retData"];
           NSString *result = [NSString stringWithFormat:@"%@\n日期:%@\n时间:%@\n天气情况:%@\n气温:%@°C\n最低气温:%@°C~%@°C\n风力:%@",dict1[@"city"],dict1[@"date"],dict1[@"time"],dict1[@"weather"],dict1[@"temp"],dict1[@"l_tmp"],dict1[@"h_tmp"],dict1[@"WS"]];
           
           successBlock(result);
       }
   }];
}

+ (void)requestCurrencyByCamount:(NSString *)camount
                       fromMoney:(NSString *)fromMoney
                         toMoney:(NSString *)toMoney
                    successBlock:(void (^)(NSString *result))successBlock
                       failBlock:(void (^)(NSError *error))failBlock
{
    NSString *httpUrl = @"http://apis.baidu.com/netpopo/exchange/convert";
    NSString *httpArg = [NSString stringWithFormat:@"from=%@&to=%@&amount=%@",fromMoney,toMoney,camount];
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, httpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"ff357caeef2d506f0c9c88651a4a7e9f" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue]completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
       if (error) {
           NSError *error1 = [NSError errorWithDomain:@"com.niit.apiweak" code:1 userInfo:@{NSLocalizedDescriptionKey:error}];
           failBlock(error1);
       } else {
           NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
           NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
           NSLog(@"HttpResponseCode:%ld", responseCode);
           NSLog(@"HttpResponseBody %@",responseString);
           NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
           NSDictionary *dict1 = dict[@"result"];
           NSString *str = [NSString stringWithFormat:@"从%@到%@\n时间:%@\n汇率:%@\n换算后%@=%@",dict1[@"fromname"],dict1[@"toname"],dict1[@"updatetime"],dict1[@"rate"],camount,dict1[@"camout"]];
           successBlock(str);
       }
   }];
}
@end
